﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_BaiViet : System.Web.UI.Page
{
    BieumauDataContext db = new BieumauDataContext();
    /// <summary>
    /// hàm page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login_admin"] == null)
        {
            Response.Redirect("Index.aspx");
        }
        if (!IsPostBack)
        {
            
            //đổ dữ liệu lên gridview
            fill_data(_grid_bantin);
          
        }
        
            
    }
    /// <summary>
    /// hàm phân trang với gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _grid_bantin_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        _grid_bantin.PageIndex = e.NewPageIndex;
        fill_data(_grid_bantin);

    }
    /// <summary>
    /// Hàm đổ dữ liệu vào gridview
    /// </summary>
    /// <param name="gridview">tên gridview cần đổ dt vào</param>
    public void fill_data(GridView gridview)
    {
        
        var i = from p in db.tblBanTins orderby p.IDTin descending select p;
        gridview.DataSource = i;
        gridview.DataBind();
    }
    /// <summary>
    /// hàm xóa bản tin
    /// </summary>
    /// <param name="id"></param>
    public void delete_row(int id)
    {
       
        var d = (from p in db.tblBanTins where p.IDTin == id select p).Single();
        db.tblBanTins.DeleteOnSubmit(d);
        db.SubmitChanges();
        

    }
    /// <summary>
    /// Gọi sự kiện xóa
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void _grid_bantin_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int idmuc = Convert.ToInt32(_grid_bantin.Rows[e.RowIndex].Cells[1].Text);
        delete_row(idmuc);
        fill_data(_grid_bantin);
     
    }
    /// <summary>
    /// Btn xóa
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_xoa_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in _grid_bantin.Rows)
        {
            CheckBox checkbox = (CheckBox)row.FindControl("chkBxSelect");

            //Nếu bản ghi được chọn thì thực hiện xóa
            if (checkbox.Checked)
            {
                int idmuc = Convert.ToInt32(_grid_bantin.DataKeys[row.RowIndex].Value);
                delete_row(idmuc);


            }
        }
        fill_data(_grid_bantin);
    }
    /// <summary>
    /// btn đăng bản tin
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDangBT_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DateTime dt = DateTime.Now;
            tblBanTin bantin = new tblBanTin();
            bantin.TieuDe = txtbantin.Text;
            bantin.NoiDung = FCK_bantin.Value;
            bantin.NgayDang =dt;
            bantin.NoiDungTomTat = txtnoidungtomtat.Text;
            bantin.LoaiTin = false;
            db.tblBanTins.InsertOnSubmit(bantin);
            db.SubmitChanges();
            fill_data(_grid_bantin);
            xoathongtin();
            Response.Redirect(Request.Url.AbsoluteUri);
        }
        
    }
    /// <summary>
    /// hàm xóa trắng các trường nhập liệu
    /// </summary>
    public void xoathongtin()
    {
        txtbantin.Text = "";
        txtbantin.Focus();
        FCK_bantin.Value = "";
    }

   


}